// pages/login/index.js
//获取应用实例
const app = getApp();
var baseurl = app.baseurl.url;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    yuming: app.yuming.url,
    logo: '',
    distribution_id: ''
  },
  denlu: function () { },
  gologin() {
    var that = this
    wx.showLoading({
      icon: "loading",
      title: '加载中...',
      duration: 1000
    })

    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (obj) => {
        console.log("111", obj)
        // console.log(obj.userInfo.avatarUrl);
        wx.setStorageSync('avatarUrl', obj.userInfo.avatarUrl)
        wx.setStorageSync('user_name', obj.userInfo.nickName)
        wx.login({ //必须先获取信息后再调用login，再login里调用信息接口调用不了
          complete: (loginResult) => {
            console.log("222", loginResult)
            console.log(loginResult.code);
            if (loginResult.code != '') {
              //发起网络请求 获取openid
              wx.request({
                url: `${baseurl}/user.Login/get_code`,
                data: {
                  code: loginResult.code
                },
                success(res) {
                  // console.log(res)
                  // console.log("333", res.data)

                  wx.setStorageSync('openid', res.data.data.openid)
                  // console.log(wx.getStorageSync("openid"));
                  // 登录
                  wx.request({
                    url: `${baseurl}/user.Login/get_user`,
                    data: {
                      nick_name: obj.userInfo.nickName,
                      avatar_url: obj.userInfo.avatarUrl,
                      distribution_id: that.data.distribution_id,
                      openid: wx.getStorageSync("openid"),
                      is_logins: 'mingchuan'
                    },
                    success(resed) {
                      console.log(resed)
                      wx.showToast({
                        title: res.data.msg,
                        icon: 'none',
                        duration: 2000
                      })
                      wx.setStorageSync('user_id', resed.data.data)
                      // console.log(wx.getStorageSync("user_id"));
                      wx.reLaunch({
                        url: '/pages/index/index'
                      })
                    }
                  })
                }
              })
            } else {
              console.log('登录失败！' + loginResult.errMsg)
              wx.showToast({
                title: '登录失败！',
                icon: 'none',
                duration: 2000
              })
            }

          },
        })
      }
    })
  },
  agreeGetUser: function (e) {
    wx.login({
      success: function (loginResult) {
        if (loginResult.code) {
          //发起网络请求
          // wx.request({
          //   url: `${baseurl}api/Login/get_code`,
          //   data: {
          //     code: loginResult.code
          //   },
          //   success(res) {
          //     // console.log(res.data.data)
          //     wx.setStorageSync('openid', res.data.data.openid)
          //     // console.log(wx.getStorageSync("openid"));
          //   }
          // })
        } else {
          // console.log('登录失败！' + loginResult.errMsg)
        }
        console.log(loginResult);
        if (loginResult.code) {
          wx.getSetting({
            success: (res) => {
              if (!res.authSetting['scope.userInfo']) {
                wx.showToast({
                  title: '拒绝授权将不能体验小程序完整功能',
                  icon: 'none'
                })
              } else {
                wx.getUserInfo({
                  withCredentials: true,
                  success: res => {
                    // 用户信息
                    // console.log(res);
                    let encryptedData = res.encryptedData;
                    let iv = res.iv;
                    let nickName = res.userInfo.nickName
                    let avatarUrl = res.userInfo.avatarUrl
                    // console.log(res.userInfo.nickName);
                    // console.log(res.userInfo.avatarUrl);
                    //发起网络请求
                    wx.request({
                      url: `${baseurl}api/Login/get_user`,
                      data: {
                        realname: res.userInfo.nickName,
                        head_img: res.userInfo.avatarUrl,
                        openid: wx.getStorageSync("openid")
                      },
                      success(res) {
                        // console.log(res)
                        wx.setStorageSync('id', res.data.data)
                        // console.log(wx.getStorageSync("id"));
                        wx.reLaunch({
                          url: '/pages/index/index'
                        })
                      }
                    })

                    // // 请求登录接口
                    // app.HttpService.signIn({
                    //   encryptedData: encryptedData,
                    //   iv: iv,
                    //   code: loginResult.code
                    // }).then(data => {
                    //   // 请求成功
                    //   storage.set("userInfo", data, 180);
                    //   storage.set("token", data.data.token, 180);
                    //   // 缓存数据
                    //   wx.reLaunch({
                    //     url: '/pages/index/index'
                    //   })
                    // }).catch(e => { })
                  }
                })

              }
            }
          })
        } else {
          wx.showToast({
            title: '登陆失败！',
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(options.scene);
    if (options.scene == null) {
      this.setData({
        distribution_id: 0
      })
    } else {
      this.setData({
        distribution_id: options.scene
      })
    }


    wx.request({
      url: `${baseurl}/user.System/project_details`,
      method: 'POST',
      data: {
        id: 1
      },
      success: (res) => {
        console.log(res.data.data);
        this.setData({
          logo: res.data.data
        })
      }
    })


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})